<?php

defined('SYSPATH') or die('Access error!');

/**
 *  信息提示
 */
class Message
{
    /**
     * 常用的变量
     */

    const ERROR = 'error';
    const ALERT = 'alert';
    const CRITICAL = 'critical';
    const NOTICE = 'notice';
    const SUCCESS = 'success';
    const WARN = 'warning';
    const INFO = 'info';
    const ACCESS = 'access';
    const DEBUG = 'debug';

    /**
     * @var  session的key
     */
    public static $session_key = 'messages';

    /**
     * 添加一个消息
     *
     * @param   string  message type (e.g. Message::SUCCESS)
     * @param   string  Array/String for the message(s)
     * @param   array   any options for the message
     * @return  void
     */
    public static function set($type, $message, array $options = NULL)
    {
        $messages = (array) self::get();

        if (!is_array($messages))
        {
            $messages = array();
        }

        if (is_array($message))
        {
            foreach ($message as $_type => $_message)
            {
                $messages[] = array(
                    'type' => $type,
                    'text' => $_message,
                    'options' => (array) $options,
                );
            }
        }
        else
        {
            $messages[] = array(
                'type' => $type,
                'text' => $message,
                'options' => (array) $options,
            );
        }

        Session::instance()->set(self::$session_key, $messages);
    }

    //错误提示
    public static function error($message, array $options = NULL)
    {
        self::set(self::ERROR, $message, $options);
    }

    //成功提示
    public static function success($message, array $options = NULL)
    {
        self::set(self::SUCCESS, $message, $options);
    }

    //信息提示
    public static function info($message, array $options = NULL)
    {
        self::set(self::INFO, $message, $options);
    }

    public static function get($type = NULL, $default = NULL, $delete = TRUE)
    {
        $messages = Session::instance()->get(self::$session_key, array());

        if ($messages === NULL)
        {
            return $default;
        }

        if ($type !== NULL)
        {
            $return = array();

            $remainder = array();

            foreach ($messages as $message)
            {
                if (($message['type'] === $type) OR (is_array($type) AND in_array($message['type'], $type)) OR (is_array($type) AND Arr::is_assoc($type) AND !in_array($message['type'], $type[1])))
                {
                    $return[] = $message;
                }
                else
                {
                    $remainder[] = $message;
                }
            }

            if (empty($return))
                return $default;

            $messages = $return;
        }

        if ($delete === TRUE)
        {
            if ($type === NULL OR empty($remainder))
            {
                self::clear();
            }
            else
            {
                Session::instance()->set(self::$session_key, $remainder);
            }
        }

        return $messages;
    }

    public static function clear($type = NULL)
    {
        if ($type === NULL)
        {
            Session::instance()->delete(self::$session_key);
        }
        else
        {
            self::get($type, NULL, TRUE);
        }
    }

}